package com.kikikeji.weizhuo.activity.core;

import android.app.Application;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;

import com.kikikeji.weizhuo.util.ActivityUtils;
import com.kikikeji.weizhuo.util.ViewFinder;
import com.kikikeji.weizhuo.util.ViewUtils;

public abstract class BaseCoreActivity extends FragmentActivity {

    protected ViewFinder mViewFinder;

    private ActivityStatusIntf mStatusHelper;

    private boolean isPushStack;

    private boolean mActivityDestroyed;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mViewFinder = new ViewFinder(this);

        if (getApplication() instanceof ActivityManageIntf) {
            mStatusHelper = ((ActivityManageIntf) getApplication()).getActivityStatusHelper();
        }

        if (mStatusHelper != null) {
            mStatusHelper.onCreate(getIntent());
        }

        isPushStack = ActivityUtils.pushStack(this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

        if (mStatusHelper != null) {
            mStatusHelper.onNewIntent(intent);
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mActivityDestroyed = true;
        if (isPushStack) {
            ActivityUtils.popStack(this);
        }
    }

    public ViewFinder getViewFinder() {
        return mViewFinder;
    }

    protected BaseCoreActivity show(final View view) {
        ViewUtils.setGone(view, false);
        return this;
    }

    protected BaseCoreActivity hide(final View view) {
        ViewUtils.setGone(view, true);
        return this;
    }

    @Override
    protected void onResume() {
        super.onResume();
        Application application = getApplication();

        if (application != null && application instanceof ActivityManageIntf)
            ((ActivityManageIntf) application).setCurActivity(this);
    }

    @Override
    protected void onPause() {
        super.onPause();

        Application application = getApplication();

        if (application != null && application instanceof ActivityManageIntf)
            ((ActivityManageIntf) application).setCurActivity(null);
    }

    @Override
    public void onBackPressed() {
        try {
            super.onBackPressed();

            if (getSupportFragmentManager().getBackStackEntryCount() <= 1) {
                onBackEvent();
            }
        } catch (Exception e) {
            String msg = "Class:" + getClass().getName() + "  Msg:" + e.getMessage();
            finish();
            onBackEvent(msg);
        }
    }

    protected void onBackEvent(Object... params) {
        if (mStatusHelper != null)
            mStatusHelper.onActivityBack(params);
    }

    public boolean isActivityDestroyed() {
        return mActivityDestroyed;
    }

}
